package com.huanuo.internetofthings.service.impact.statistics;

import java.util.Calendar;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.unicorn.framework.base.base.AbstractService;
import org.unicorn.framework.core.ResponseDto;
import org.unicorn.framework.core.SysCode;
import org.unicorn.framework.core.exception.PendingException;
import org.unicorn.framework.core.handler.IHandler;

import com.huanuo.internetofthings.bo.impact.ImpactMonthStatistics;
import com.huanuo.internetofthings.bo.user.UserInfo;
import com.huanuo.internetofthings.dto.impact.ImpactDeviceStatisticsReqDto;
import com.huanuo.internetofthings.service.impact.ImpactDeviceMonthStatisticsService;
import com.huanuo.internetofthings.service.impact.ImpactDeviceStatisticsService;
import com.huanuo.internetofthings.service.user.UserInfoService;

public abstract  class AbstractImpactDeviceStatisticsService extends AbstractService  implements IHandler<ImpactDeviceStatisticsReqDto>{

	@Autowired
	public ImpactDeviceStatisticsService impactDeviceStatisticsService;
	@Autowired
	public ImpactDeviceMonthStatisticsService impactDeviceMonthStatisticsService;
	@Autowired
	public UserInfoService userInfoService;
	
	
	/**
	 * 查询条件
	 * @param userInfo
	 * @return
	 * @throws PendingException
	 */
    public abstract ImpactMonthStatistics getImpactMonthStatistics(UserInfo userInfo) throws PendingException;
    /**
     * 计算统计
     * @param impactMonthStatisticsList
     * @param userInfo
     * @return
     * @throws PendingException
     */
    public abstract ResponseDto<?>  calculationStatistics(List<ImpactMonthStatistics> impactMonthStatisticsList,UserInfo userInfo) throws PendingException;
	/**
	 * 
	 */
     public  ResponseDto<?> execute(ImpactDeviceStatisticsReqDto impactDeviceStatisticsReqDto) throws PendingException{
    	 
    	 return statistics(impactDeviceStatisticsReqDto);
     }
    
    /**
	 * 统计
	 * @param impactDeviceStatisticsReqDto
	 * @return
	 * @throws PendingException
	 */
	public ResponseDto<?> statistics(ImpactDeviceStatisticsReqDto impactDeviceStatisticsReqDto) throws PendingException {
		try {
			// 根据用户标识查询用户信息
			UserInfo userInfo = checkUser(impactDeviceStatisticsReqDto.getUserIdentity());
			//获取记录列表
			List<ImpactMonthStatistics> impactMonthStatisticsList = impactDeviceMonthStatisticsService.getImpactMonthStatisticsList(getImpactMonthStatistics(userInfo));
			//返回
			return calculationStatistics(impactMonthStatisticsList,userInfo);
		} catch (PendingException pe) {
			throw pe;
		} catch (Exception e) {
			error("统计失败", e);
			throw new PendingException(SysCode.SYS_FAIL, "统计失败");
		}
	}
  /**
   * 检查用户
   * @param userIdentity
   * @return
   * @throws PendingException
   */
	public UserInfo checkUser(String userIdentity) throws PendingException {
		// 根据用户标识查询用户信息
		UserInfo userInfo = userInfoService
				.getUserInfoByCondition(UserInfo.builder().userIdentity(userIdentity).build());
		if (userInfo == null) {
			throw new PendingException(SysCode.SESSION_ERROR);
		}
		if (StringUtils.isBlank(userInfo.getGroupNo())) {
			throw new PendingException(SysCode.SYS_FAIL, "用户不属于任何组");
		}
		return userInfo;
	}
	/**
	 * 获取有序map
	 * @param clazz
	 * @return
	 */
	public <T> Map<Integer, T> getSortMap(Class<T> clazz) {
		// 按key升序
		Map<Integer, T> thisPeriodMap = new TreeMap<Integer, T>(new Comparator<Integer>() {
			public int compare(Integer obj1, Integer obj2) {
				// 排序
				return obj1.compareTo(obj2);
			}
		});
		return thisPeriodMap;
	}
	/**
	 * 校验
	 */
	@Override
	public void vaildation(ImpactDeviceStatisticsReqDto s) throws PendingException {
		if(StringUtils.isBlank(s.getUserIdentity())){
			throw new PendingException(SysCode.SESSION_ERROR);
		}
		if(s.getImpactDeviceStatisticsType()==null){
			throw new PendingException(SysCode.SYS_FAIL,"统计类型不能为空");
		}
	}
	
	public Integer getYear(){
		return  Calendar.getInstance().get(Calendar.YEAR);
	}
	
}